package com.songlanyun.modules.notice.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.songlanyun.common.enums.HouniaoConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.HouniaoApiUtil;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import com.songlanyun.modules.goods.model.values.GoodsSkuTableItem;
import com.songlanyun.modules.notice.entity.GoodsNotice;
import com.songlanyun.modules.notice.entity.NoticeData;
import com.songlanyun.modules.notice.service.GoodsNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("api/houniao")
public class GoodsNoticeController{

    /**
     * shiro权限校验
     * 配置文件 登录拦截器
     */

    @Autowired
    private GoodsNoticeService goodsNoticeService;

    @Autowired
    private HouniaoApiUtil houniaoApiUtil;

    /**
     * 测试
     */
    @PostMapping("/test/{spu}")
    public R test(@PathVariable("spu") String spu){
        goodsNoticeService.add(spu);
        return R.ok();
    }

    //聚水潭发货回调
    @RequestMapping(value = "jstNotifyUrl", method = RequestMethod.POST)
    @ResponseBody
//    public JSONObject jstNotifyUrl(@RequestBody JSONObject jsData) {
//        HashMap<String,Object> result = new HashMap();
//        result.put("code", "0");
//        result.put("msg", "成功");
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("code", "0");
//        jsonObject.put("msg", "成功");
//        return jsonObject;
//    }
    public JSONObject jstNotifyUrl(@RequestBody JSONObject jsonData) {
        System.out.println("=================聚水潭回调================");
        System.out.println(jsonData);
        JSONObject jsonObject = goodsNoticeService.orderJstSend(jsonData);
        // 处理接收到的JSON数据
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("code", "0");
//        jsonObject.put("msg", "成功");
        return jsonObject;
    }

    /**
     * 商品回调接口
     */
    @RequestMapping("/notice")
    public R add(@RequestBody JSONObject params){
        log.info("接收到候鸟回调：{}", params);
        // 获取 商品spu type
        JSONObject data = params.getJSONObject("data");
        String goods_spu = data.getString("goods_spu");
        String type = params.getString("type");
        String msg = null;
        // 判断商品操作类型
        switch (type) {
            case "goods.add":
                goodsNoticeService.add(goods_spu);
                msg = "商品新增回调成功";
                break;
            case "goods.update":
                goodsNoticeService.update(goods_spu);
                msg = "商品更新回调成功";
                break;
            case "goods.shelve":
                goodsNoticeService.shelve(goods_spu);
                msg = "商品上架回调成功";
                break;
            case "goods.unshelve":
                goodsNoticeService.unshelve(goods_spu);
                msg = "商品下架回调成功";
                break;
            case "goods.price":
                goodsNoticeService.price(goods_spu);
                msg = "商品价格库存更新回调成功";
                break;
            case "goods.stock":
                goodsNoticeService.stock(goods_spu);
                msg = "商品~~~~库存更新回调成功";
                break;
            case "order.send":
                goodsNoticeService.orderSend(data);
                msg = "订单~~~~发货通知回调成功";
                break;
            case "order.status":
                goodsNoticeService.orderStatus(data);
                msg = "订单~~~~状态更新通知回调成功";
                break;
        }
        return R.error(200, msg);
    }

//    /**
//     * 回调转换成String
//     * @param msg
//     * @return
//     */
//    private String convertStream(String msg) {
//        R str = new R();
//        str.put("code", 200);
//        str.put("msg", msg);
//        String s = str.toString();
//        return s;
//    }
}
